package homework03;

//https://www.yiibai.com/javareflect/java_reflect_method.html#article-start
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;

public class GeneratePerson {
	public static String NameOfPersonJava = "homework03.Person";

	public static void main(String[] args) throws ClassNotFoundException {
		System.out.println(JavaFromClass(NameOfPersonJava));
	}

	@SuppressWarnings("rawtypes")
	public static String JavaFromClass(String className) throws ClassNotFoundException {
		Class clazz = Class.forName(className);
		// 不使用String"+"来拼接而是使用StringBuilder来操作字符串，避免产生大量无用的中间对象，耗费空间且执行效率低下
		StringBuilder javaContext = new StringBuilder();
		// getSimpleName()得到类的简写名称，即Person
		javaContext.append("public class " + clazz.getSimpleName() + " {\n");
		// getDeclaredFields()得到类的所有属性的描述对象
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);// 设置值为true表示反射对象应该在使用时抑制Java语言访问检查。
			javaContext.append(GetModifiers(field.getModifiers()) + " " + field.getType().getSimpleName() + " "
					+ field.getName() + ";\n");
		}
		// 类的方法
		Method[] methods = clazz.getDeclaredMethods();
		for (Method method : methods) {
			method.setAccessible(true);
			javaContext.append(GetModifiers(method.getModifiers()) + " " + method.getReturnType().getSimpleName() + " "
					+ method.getName() + "(");
			Class<?>[] parameterTypes = method.getParameterTypes();
			Parameter[] parameters = method.getParameters();
			for (int i = 0; i < parameterTypes.length; i++) {
				String simpleName = parameterTypes[i].getSimpleName();
				javaContext.append(simpleName + " " + parameters[i].getName());
			}
			javaContext.append(")");
			Class<?>[] exceptionTypes = method.getExceptionTypes();

			if (exceptionTypes.length >= 1) {
				javaContext.append("throws ");
			}
			for (int i = 0; i < exceptionTypes.length; i++) {
				if (i != 0 && i != exceptionTypes.length - 1) {
					javaContext.append(",");
				}

				javaContext.append(exceptionTypes[i].getSimpleName());
			}
			javaContext.append(" {}\n");
		}
		// 类的构造方法
		Constructor[] constructors = clazz.getConstructors();
		for (Constructor constructor : constructors) {
			constructor.setAccessible(true);
			javaContext.append(GetModifiers(constructor.getModifiers()) + " "
					+ constructor.getDeclaringClass().getSimpleName() + "(");
			Class<?>[] parameterTypes = constructor.getParameterTypes();
			Parameter[] parameters = constructor.getParameters();
			for (int i = 0; i < parameterTypes.length; i++) {
				String simpleName = parameterTypes[i].getSimpleName();
				javaContext.append(simpleName + " " + parameters[i].getName());
			}
			javaContext.append(")");
			Class<?>[] exceptionTypes = constructor.getExceptionTypes();

			if (exceptionTypes.length >= 1) {
				javaContext.append("throws ");
			}
			for (int i = 0; i < exceptionTypes.length; i++) {
				if (i != 0 && i != exceptionTypes.length - 1) {
					javaContext.append(",");
				}

				javaContext.append(exceptionTypes[i].getSimpleName());
			}
			javaContext.append(" {}\n");
		}
		javaContext.append("}\n");
		return javaContext.toString();
	}

	private static String GetModifiers(int modifiers) {
		if (modifiers == Modifier.PROTECTED) {
			return "";
		} else if (modifiers == Modifier.PUBLIC) {
			return "public";
		} else if (modifiers == Modifier.PRIVATE) {
			return "private";
		}
		return "";
	}

}
